import json

from django.http import HttpResponse
from rest_framework import status


class ResultModel():
    code = ''
    message = ''

    def __init__(self, code, msg):
        self.code = code
        self.message = msg


def SuccessResult(msg):
    return ResultModel(200, msg)


def ErrorResult(msg):
    return ResultModel(400, msg)


def getResult(_code, _msg):
    _t = ResultModel()
    _t.code = _code
    _t.message = _msg
    return _t


def convert_to_builtin_type(obj):
    d = {}
    d.update(obj.__dict__)
    return d


def SuccessHR(msg):
    return HttpResponse(json.dumps(SuccessResult(msg), default=convert_to_builtin_type, ensure_ascii=False),
                        content_type="application/json", status=status.HTTP_200_OK)


def ErrorHR(msg):
    return HttpResponse(json.dumps(ErrorResult(msg), default=convert_to_builtin_type, ensure_ascii=False),
                        content_type="application/json", status=status.HTTP_200_OK)
